package org.pmt.first.kms.screen

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import arrow.core.Either
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import moe.tlaster.precompose.molecule.producePresenter
import org.koin.compose.koinInject
import org.pmt.first.kms.data.network.HttpError
import org.pmt.first.kms.data.network.OnlineRepository

@Composable
fun TestScreen() {
    // PreComposeApp {
    val state by producePresenter { Presenter() }
    MaterialTheme {
        Scaffold {
            Column(
                modifier = Modifier.fillMaxSize(),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center,
            ) {
                Text(text = state.count)
                Button(
                    onClick = {
                        state.action(TestAction.Increment)
                    },
                ) {
                    Text(text = "Increment")
                }
                Button(
                    onClick = {
                        state.action(TestAction.Decrement)
                    },
                ) {
                    Text(text = "Decrement")
                }
            }
        }
    }
    // }
}

@Composable
fun Presenter(): TestState {
    var count by remember { mutableStateOf(0) }
    var news by remember { mutableStateOf("0") }
    val onlineRepository : OnlineRepository = koinInject()
    return TestState(
        "Clicked $count times $news",
    ) {
        when (it) {
            TestAction.Increment -> count++
            TestAction.Decrement -> {
                count--
                CoroutineScope(Dispatchers.Default).launch {
                    val result = onlineRepository.getNewsList()
                    when (result) {
                        is Either.Left -> {
                            news = if (result.value is HttpError.Unknown) "unknown" else "error"
                            println(news)
                        }

                        is Either.Right -> {
                            news = result.value.result.toString()
                            println(news)
                        }
                    }
                }
            }
        }
    }
}

sealed interface TestAction {
    data object Increment : TestAction
    data object Decrement : TestAction
}

data class TestState(
    val count: String,
    val action: (TestAction) -> Unit,
)

